package com.msmall.community.diary.util;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.Constants;
import com.msmall.base.util.FileIO;
import com.msmall.base.util.StringUtil;
import com.msmall.community.common.Configuration;

public class DiaryUtil {
	private static final Log logger = LogFactory.getLog(DiaryUtil.class);
	
	public static final String PREFIX_WML = "file/wml/user/";
	public static final String PREFIX_HTML = "file/html/user/";
	public static final String DIARY_PATH = "/diary/";
	public static final int DIARY_LIST_PAGESIZE_DEFAULT = 6;
	
	public static String[] writeDiayFile(String content, Long userid, String uat) throws IOException{
		String fileName = getFileName();
		String path = getAbsoluteHomePath(userid, uat) + fileName + Constants.FILE_HTML_EXTENDS;
		FileIO.writeFile(content, path);
		return new String[]{userid + DIARY_PATH, fileName, Constants.FILE_HTML_EXTENDS};
	}
	
	public static String getFileName(){
		return FileIO.getRandomFileName();
	}
	
	
	public static String getAbsoluteHomePath(Long userid, String uat){
		return Constants.ROOTPATH + getFolderPath(userid, uat);
	}
	/**
	 * file/html/user/id/diary/
	 */
	public static String getFolderPath(Long userid, String uat){
		return getPrefixPath(uat)+userid + DIARY_PATH;
	}
	
	public static String getPrefixPath(String uat){
		if(Constants.USER_AGENT_TYPE_HTML.equals(uat)){
			return PREFIX_HTML;
		}
		if(Constants.USER_AGENT_TYPE_WML.equals(uat)){
			return PREFIX_WML;
		}
		return PREFIX_WML;
	}
	
	
	/**
	 * 日记标题最少字符数
	 * @return
	 */
	
	public static int getDiaryTitleMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.SPACE_DIARY_TITLE_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 日记标题最多字符数
	 * @return
	 */
	public static int getDiaryTitleMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.SPACE_DIARY_TITLE_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	/**
	 * 日记内容最少字符数
	 * @return
	 */
	public static int getDiaryContentMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.SPACE_DIARY_CONTENT_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 日记内容最多字符数
	 * @return
	 */
	public static int getDiaryContentMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.SPACE_DIARY_CONTENT_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	
	/**
	 * 日记回复最少字符数
	 * @return
	 */
	public static int getDiaryReplyMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.SPACE_DIARY_REPLY_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 日记回复最多字符数
	 * @return
	 */
	public static int getDiaryReplyMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.SPACE_DIARY_REPLY_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	
	/**
	 * 判断日记标题字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkDiaryTitleLength(String title){
		if( StringUtil.nullOrBlank(title))
			return 2;
		int length = title.length();
		if(getDiaryTitleMinChars() > 0 ){
			if(length < getDiaryTitleMinChars())
				return 2;
		}
		if(getDiaryTitleMaxChars() > 0 ){
			if( length > getDiaryTitleMaxChars())
				return 3;
		}
		return 1;
	}
	/**
	 * 判断日记内容字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkDiaryContentLength(String content){
		if(StringUtil.nullOrBlank(content) )
			return 2;
		int length = content.length();
		if(getDiaryContentMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getDiaryContentMinChars())
				return 2;
		}
		if(getDiaryContentMaxChars() > 0 ){
			if(length > getDiaryContentMaxChars())
				return 3;
		}
		return 1;
	}
	/**
	 * 判断日记回复内容字符数是否在最大或最小之间
	 * @return  2:字符数太少，3字符数太多，1正常
	 */
	public static int checkDiaryReplyLength(String content){
		if(StringUtil.nullOrBlank(content))
			return 2;
		int length = content.length();
		if(getDiaryReplyMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getDiaryReplyMinChars())
				return 2;
		}
		if(getDiaryReplyMaxChars() > 0 ){
			if(length > getDiaryReplyMaxChars())
				return 3;
		}
		return 1;
	}
}
